package org.minuframework.file.upload;

import java.io.File;
import java.util.Map;

import org.minuframework.lang.StringUtils;
import org.minuframework.utils.ParamUtils;
import org.minuframework.file.FileReader;

@Deprecated
public class FileParamerConfigurer{
    
    /**
     * 前缀
     */
    private static final String prefix = "file.upload";
    
    private File file;
    
    private Map<Object, Object> params;
    
    public FileParamerConfigurer(File file){
        this.file = file;
    }
    
    public Map<Object, Object> getParamerRepository(){
        if (params == null) {
            params = FileReader.asMap(file, prefix);
        }
        return params;
    }
    
    public String getFile(FileUploadType type){
        StringBuilder result = new StringBuilder();
        if (getParamerRepository().containsKey(getKey(type.name()))) {
            String param = getParamerRepository().get(getKey(type.name())).toString();
            param = param.endsWith(File.separator) ? param : param + File.separator;
            result.append(param);
        }
        return result.toString();
    }
    
    public String getFile(String type){
        StringBuilder result = new StringBuilder();
        if (getParamerRepository().containsKey(getKey(type))) {
            String param = getParamerRepository().get(getKey(type)).toString();
            param = param.endsWith(File.separator) ? param : param + File.separator;
            result.append(param);
        }
        return result.toString();
    }
    
    public String getFile(String fileName, FileUploadType type){
        StringBuilder result = new StringBuilder();
        if (StringUtils.isNotEmpty(fileName)) {
            String path = getFile(type);
            if (StringUtils.isNotEmpty(path)) {
                result.append(path + ParamUtils.suffix(fileName).substring(1) + File.separator);
            }
        }
        return result.toString();
    }
    
    public String getFile(String fileName, String type){
        StringBuilder result = new StringBuilder();
        if (StringUtils.isNotEmpty(fileName)) {
            String path = getFile(type);
            if (StringUtils.isNotEmpty(path)) {
                result.append(path + ParamUtils.suffix(fileName).substring(1) + File.separator);
            }
        }
        return result.toString();
    }
    
    public Map<Object, Object> getParams(){
        return getParamerRepository();
    }
    
    public int size(){
        return getParamerRepository().size();
    }
    
    public String toString(){
        return getParamerRepository().toString();
    }
    
    private String getKey(String key){
        return StringUtils.isNotEmpty(key) ? (prefix + '.' + key) : null;
    }
    
}
